home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgramD2.iso / Borland / Borland C++ V5.02 / GDIDIB.PAK / STATBAR.C < prev    next >
C/C++ Source or Header  |  1997-05-06  |  9KB  |  313 lines

  1. // THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
  2. // ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
  3. // THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
  4. // PARTICULAR PURPOSE.
  5. //
  6. // Copyright (C) 1993 - 1995  Microsoft Corporation.  All Rights Reserved.
  7. //
  8. //  MODULE: statbar.c
  9. //
  10. //  PURPOSE: Handles general routines for the Barsdi sample.
  11. //
  12. //  FUNCTIONS:
  13. //    MsgTimer      - Handles the WM_TIMER messages to set the time on
  14. //                    the status bar.
  15. //    MsgMouseMove  - Moved to CLIENT.C
  16. //    MsgMenuSelect - Handle the WM_MENUSELECT message. This message will
  17. //                    enable the status bar control to update when the user
  18. //                    moves across menu items on the main window.
  19. //    InitializeStatusBar - Sets the pane positions in the statusbar control
  20. //    CreateSBar    - Calls CreateStatusWindow() to create the status bar
  21. //    UpdateStatusBar - Updates the statusbar control with appropriate text
  22. //
  23.  
  24. #include <windows.h>            // required for all Windows applications
  25. #include <windowsx.h>
  26. #include <commctrl.h>           // prototypes and defs for common controls
  27. #include "globals.h"            // prototypes specific to this application
  28. #include "resource.h"
  29. #include "statbar.h"            // prototypes specific to statbar.c
  30.  
  31.             
  32. // Global Variables for the status bar control.
  33.  
  34. HWND  hWndStatusbar;
  35.  
  36. //  **TODO**  Add entries to the string table in barsdi.rc for each menu
  37. //            command.  MsgMenuSelect (below) loads these strings to display
  38. //            information in the status bar.  MsgMenuSelect assumes that the
  39. //            string ID is the same as the command ID and that a string
  40. //            exists for every command.
  41. //
  42. // The following array contains resource string ID's for popup menus
  43. // in the main application menu.  This array is used by MsgMenuSelect
  44. // to display information in the status bar.
  45. //
  46. //  **TODO**  Add entries to this array for each popup menu in the same
  47. //            positions as they appear in the main menu.  Remember to define
  48. //            the ID's in globals.h and add the strings to barsdi.rc.
  49.  
  50. UINT idPopup[] =
  51. {
  52.     IDM_FILEMENU,    
  53.     IDM_INFO,
  54. //    IDM_EDITMENU,
  55.     IDM_DRAWMENU,
  56.     IDM_HELPMENU,
  57. };
  58.  
  59.  
  60. //
  61. //  FUNCTION: MsgTimer(HWND, UINT, WPARAM, LPARAM)
  62. //
  63. //  PURPOSE: Calls GetLocalTime() to set the time on the status bar
  64. //
  65. //
  66. //  PARAMETERS:
  67. //
  68. //    hwnd      - Window handle  (Unused)
  69. //    uMessage  - Message number (Unused)
  70. //    wparam    - Extra data     (Unused)
  71. //    lparam    - Extra data     (Unused)
  72. //
  73. //  RETURN VALUE:
  74. //
  75. //    Always returns 0 - Message handled
  76. //
  77. //  COMMENTS:
  78. //
  79. //    Every time the window procedure receives a Timer message, it calls
  80. //    GetLocalTime() to obtain the time and then formats the time into
  81. //    a string. The time sting is then displayed on the status bar.
  82. //
  83.  
  84. #pragma argsused
  85. LRESULT MsgTimer(HWND hwnd, UINT uMessage, WPARAM wparam, LPARAM lparam)
  86. {
  87.     char        szBuf[16];      // Temp buffer.
  88.     SYSTEMTIME  sysTime;
  89.  
  90.  
  91.     GetLocalTime(&sysTime);
  92.  
  93.     wsprintf(szBuf,
  94.              "%2d:%02d:%02d %s",
  95.              (sysTime.wHour == 0 ? 12 :
  96.              (sysTime.wHour <= 12 ? sysTime.wHour : sysTime.wHour -12)),
  97.              sysTime.wMinute,
  98.              sysTime.wSecond,
  99.              (sysTime.wHour < 12 ? "AM":"PM"));
  100.  
  101.     UpdateStatusBar(szBuf, 4, 0);
  102.     return 0;
  103. }
  104.  
  105.  
  106. //
  107. //  FUNCTION: MsgMenuSelect(HWND, UINT, WPARAM, LPARAM)
  108. //
  109. //  PURPOSE:  Upadates menu selections on the staus bar.
  110. //
  111. //
  112. //  PARAMETERS:
  113. //
  114. //    hwnd      - Window handle  (Used)
  115. //    uMessage  - Message number (Used)
  116. //    wparam    - Extra data     (Used)
  117. //    lparam    - Extra data     (Used)
  118. //
  119. //  RETURN VALUE:
  120. //
  121. //    Always returns 0 - Message handled
  122. //
  123. //  COMMENTS:
  124. //    This message is sent when the user selects menu items by
  125. //    by pulling down  a popup menu move the mouse around to highlite
  126. //    different menu items.
  127. //
  128. //
  129.  
  130. #pragma argsused
  131. LRESULT MsgMenuSelect(HWND hwnd, UINT uMessage, WPARAM wparam, LPARAM lparam)
  132. {
  133.      static char szBuffer[128];
  134.      UINT   nStringID;
  135.      UINT   fuFlags = GET_WM_MENUSELECT_FLAGS(wparam, lparam) & 0xffff;
  136.      UINT   uCmd    = GET_WM_MENUSELECT_CMD(wparam, lparam);
  137.      HMENU  hMenu   = GET_WM_MENUSELECT_HMENU(wparam, lparam);
  138.  
  139.  
  140.      szBuffer[0] = 0;                            // First reset the buffer
  141.  
  142.  
  143.      if (fuFlags == 0xffff && hMenu == NULL)     // Menu has been closed
  144.           nStringID = IDS_DESCRIPTION;
  145.  
  146.      else if (fuFlags & MFT_SEPARATOR)           // Ignore separators
  147.           nStringID = 0;
  148.  
  149.      else if (fuFlags & MF_POPUP)                // Popup menu
  150.      {
  151.           if (fuFlags & MF_SYSMENU)               // System menu
  152.                 nStringID = IDS_SYSMENU;
  153.  
  154.           else
  155.                 // Get string ID for popup menu from idPopup array.
  156.                 nStringID = ((uCmd < sizeof(idPopup)/sizeof(idPopup[0])) ?
  157.                                      idPopup[uCmd] : 0);
  158.      }  // for MF_POPUP
  159.  
  160.      else                                        // Must be a command item
  161.           nStringID = uCmd;                       // String ID == Command ID
  162.  
  163.      // Load the string if we have an ID
  164.     if (0 != nStringID)
  165.         LoadString(hInst, nStringID, szBuffer, sizeof(szBuffer));
  166.  
  167.     // Finally... send the string to the status bar
  168.     UpdateStatusBar(szBuffer, 0, 0);
  169.  
  170.      return 0;
  171. }
  172.  
  173.  
  174. //
  175. //  FUNCTION: InitializeStatusBar(HWND)
  176. //
  177. //  PURPOSE:  Initialize statusbar control with time and mouse positions.
  178. //
  179. //
  180. //  PARAMETERS:
  181. //
  182. //  hwndParent - Window handle of the status bar's parent
  183. //
  184. //
  185. //  RETURN VALUE:  NONE
  186. //
  187. //
  188. //  COMMENTS:
  189. //
  190. //   This function initializes the time  and mouse positions sections of
  191. //   the statubar window. The Date for the time section is obtained by
  192. //   calling SetTimer API. When the timer messages start comming in,
  193. //   GetSytemTime() to fill the time section.
  194. //   The WPARAM of SB_SETTEXT is divided into 2 parameters. The LOWORD
  195. //   determines which section/part the text goes into, and the HIWORD
  196. //   tells how the bar is drawn (popin or popout).
  197. //
  198.  
  199. void InitializeStatusBar(HWND hwndParent)
  200. {
  201.     const cSpaceInBetween = 8;
  202.     int   ptArray[5];   // Array defining the number of parts/sections
  203.     SIZE  size;         // the Status bar will display.
  204.     RECT  rect;
  205.     HDC   hDC;
  206.  
  207.    /*
  208.     * Fill in the ptArray...
  209.     */
  210.  
  211.     hDC = GetDC(hwndParent);
  212.     GetClientRect(hwndParent, &rect);
  213.  
  214.     ptArray[4] = rect.right;
  215.  
  216.     if (GetTextExtentPoint(hDC, "00:00:00 PM", 12, &size))
  217.         ptArray[3] = ptArray[4] - (size.cx) - cSpaceInBetween;
  218.     else
  219.         ptArray[3] = 0;
  220.  
  221.     if (GetTextExtentPoint(hDC, "Time:", 6, &size))
  222.         ptArray[2] = ptArray[3] - (size.cx) - cSpaceInBetween;
  223.     else
  224.         ptArray[2] = 0;
  225.  
  226.     if (GetTextExtentPoint(hDC, "123,123", 8, &size))
  227.         ptArray[1] = ptArray[2] - (size.cx) - cSpaceInBetween;
  228.     else
  229.         ptArray[1] = 0;
  230.  
  231.     if (GetTextExtentPoint(hDC, "Cursor Pos:", 12, &size))
  232.         ptArray[0] = ptArray[1] - (size.cx) - cSpaceInBetween;
  233.     else
  234.         ptArray[0] = 0;
  235.  
  236.     ReleaseDC(hwndParent, hDC);
  237.  
  238.     SendMessage(hWndStatusbar,
  239.                 SB_SETPARTS,
  240.                 sizeof(ptArray)/sizeof(ptArray[0]),
  241.                 (LPARAM)(LPINT)ptArray);
  242.  
  243.     UpdateStatusBar(SZDESCRIPTION, 0, 0);
  244.     UpdateStatusBar("Cursor Pos:", 1, SBT_POPOUT);
  245.     UpdateStatusBar("Time:", 3, SBT_POPOUT);
  246. }
  247.  
  248.  
  249. //
  250. //  FUNCTION: CreateSBar(HWND, UINT, WPARAM, LPARAM)
  251. //
  252. //  PURPOSE:  Calls CreateStatusWindow() to create the status bar
  253. //
  254. //
  255. //  PARAMETERS:
  256. //
  257. //  hwndParent - Window handle of the status bar's parent
  258. //
  259. //  RETURN VALUE:
  260. //
  261. //  If both controls were created successfully Return TRUE,
  262. //  else returns FALSE.
  263. //
  264. //  COMMENTS:
  265. //
  266. //
  267.  
  268. BOOL CreateSBar(HWND hwndParent)
  269. {
  270.     hWndStatusbar = CreateStatusWindow(WS_CHILD | WS_VISIBLE | WS_BORDER,
  271.                                        SZDESCRIPTION,
  272.                                        hwndParent,
  273.                                        IDM_STATUSBAR);
  274.     if(hWndStatusbar)
  275.     {
  276.         InitializeStatusBar(hwndParent);
  277.         SetTimer(hwndParent, IDM_TIMER, TIMER_TIMEOUT, NULL);
  278.         return TRUE;
  279.     }
  280.  
  281.     return FALSE;
  282. }
  283.  
  284.  
  285. //
  286. //  FUNCTION: UpdateStatusBar(HWND)
  287. //
  288. //  PURPOSE:  Updates the statusbar control with appropriate text
  289. //
  290. //
  291. //  PARAMETERS:
  292. //
  293. //  lpszStatusString - text to be displayed
  294. //  partNumber       - which part of the status bar to display text in
  295. //  displayFlags     - display flags
  296. //
  297. //
  298. //  RETURN VALUE: NONE
  299. //
  300. //
  301. //  COMMENTS:
  302. //      None
  303. //
  304. //
  305.  
  306. void UpdateStatusBar(LPSTR lpszStatusString, WORD partNumber, WORD displayFlags)
  307. {
  308.     SendMessage(hWndStatusbar,
  309.                 SB_SETTEXT,
  310.                 partNumber | displayFlags,
  311.                 (LPARAM)lpszStatusString);
  312. }
  313.